#include "util.h"
#include "memrec.h"
#include <direct.h>
#include <algorithm>
#define new DBG_NEW

#ifndef _linux_
void DumpToFile(char * str)
{
	DWORD id = GetCurrentProcessId();
#ifdef _DUMP_LOG
	char fn[256];
	sprintf(fn, "%d.txt", id);
	FILE * pF = fopen(fn, "a");
	if (pF)
	{
		fprintf(pF, "%s", str);
		fclose(pF);
	}
#endif
}

std::string WideCharToMultiChar(std::wstring &widestr)
{
	std::string charstr;
	int requiresize = WideCharToMultiByte(CP_ACP, 0, widestr.c_str(), -1, NULL,
											0, NULL, NULL);
	if (requiresize == 0)
	{
		return charstr;
	}
	char * stringbuffer = new char[requiresize];
	WideCharToMultiByte(CP_ACP, 0, widestr.c_str(), -1, stringbuffer, requiresize, NULL, NULL);
	charstr = stringbuffer;
	delete[] stringbuffer;
	return charstr;
}

std::wstring MultiCharToWideChar(std::string &str)
{
	std::wstring widestr;
	int requiresize = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL,
											0);
	if (requiresize == 0)
	{
		return widestr;
	}
	wchar_t * wstringbuffer = new wchar_t[requiresize];
	MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wstringbuffer, requiresize);
	widestr = wstringbuffer;
	delete[] wstringbuffer;
	return widestr;
}

std::wstring GetModulePath()
{
	WCHAR ModulePath[MAX_PATH];
	GetModuleFileNameW(NULL, ModulePath, MAX_PATH);
	unsigned int si = lstrlenW(ModulePath);
	while (ModulePath[si] != L'\\' && si > 0)
	{
		si--;
	}
	ModulePath[si] = 0;
	return std::wstring(ModulePath);
}

std::wstring Util_GetModuleBaseName()
{
	WCHAR ModulePath[MAX_PATH];
	GetModuleFileNameW(NULL, ModulePath, MAX_PATH);
	unsigned int si = lstrlenW(ModulePath);
	while (ModulePath[si] != L'\\' && si > 0)
	{
		si--;
	}
	return std::wstring(ModulePath + si + 1);
}

std::wstring GetTempFile(const std::wstring& subfolder)
{
	WCHAR temppath[256];
	WCHAR tempfile[256];
	GetTempPathW(256, temppath);
	wcsncat_s(temppath, 256, subfolder.c_str(), 256);
	if (temppath[lstrlen(temppath)-1] != L'\\')
		wcsncat_s(temppath, 256, L"\\", 1);
	CreateDirectoryW(temppath, NULL);
	GetTempFileNameW(temppath, L"tmp", 0, tempfile);
	std::wstring str = tempfile;
	return str;
}

std::wstring CreateTempFolder(const std::wstring & subfolder)
{
	WCHAR temppath[256];
	GetTempPathW(256, temppath);
	wcsncat_s(temppath, 256, subfolder.c_str(), 256);
	if (temppath[lstrlen(temppath)-1] != L'\\')
		wcsncat_s(temppath, 256, L"\\", 1);
	CreateDirectoryW(temppath, NULL);
	std::wstring str = temppath;
	return str;
}
bool CreateFolder(std::wstring filename)
{
	if (filename[filename.size()-1] != L'\\')
		filename.append(L"\\");
	std::wstring::iterator it_slash = filename.begin();
	while (it_slash != filename.end())
	{
		it_slash = std::find(it_slash, filename.end(), L'\\');
		if (it_slash != filename.end())
		{
			std::wstring dir = filename.substr(0, it_slash - filename.begin());
			_wmkdir(dir.c_str());
			it_slash++;
		}
	}
	return true;
}
bool DeleteFolderTree(std::wstring folder, std::wstring stopfolder)
{
	if (folder[folder.size() -1] != L'\\')
		folder.append(L"\\");
	std::wstring::size_type pos = folder.find_last_of(L'\\');
	while (pos != folder.npos)
	{
		folder = folder.substr(0, pos);
		if (folder == stopfolder)
			return true;
		if (!RemoveDirectory(folder.c_str()))
		{
			return false;
		}
		pos = folder.find_last_of(L"\\"); 
	}
	return true;
}
std::string WideCharToUTF8(std::wstring& str)
{
	int length = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, FALSE);
	char * str_v = new char[length+10];
	WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, str_v, length, NULL, FALSE);
	std::string ret_str = str_v;
	delete[] str_v;
	return ret_str;
}
std::wstring UTF8ToWideChar(std::string & str)
{
	int length = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
	wchar_t *str_w = new wchar_t[length+10];
	MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, str_w, length);
	std::wstring ret_str = str_w;
	delete[] str_w;
	return ret_str;
}
#endif